﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dapper;
using Dapper.Contrib.Extensions;

namespace ConsoleApp.Dapper
{
    public interface IDayRepository : IRepository<Day>
    {

    }

    public class DayRepository : DBRepository<Day>, IDayRepository
    {
        public DayRepository(IDbConnection dbconn)
            : base(dbconn)
        {

        }

        public override Day GetById(int id)
        {
            string sql = "select * from day where AutoId = " + id;
            return _dbconn.Query<Day>(sql).FirstOrDefault();
        }

        public override List<Day> GetAll()
        {
            return _dbconn.Query<Day>("select * from day").ToList();
        }

        public override void Add(Day item)
        {
            string sql = string.Format(@"INSERT INTO day (AutoId,DayName) VALUES({0},""{1}"");SELECT @@identity;", item.AutoId, item.DayName);
            int i = _dbconn.Query<int>(sql).FirstOrDefault();
            item.AutoId = i;
        }
    }
}
